package iha.auh.kds.aba.painratingapp;

import iha.auh.kds.aba.painratingapp.Models.ChildModel;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class AdvancedPainRatingFragment extends Fragment {
	private View view;
	private TextView totalSumTextView;
	
	private int group1Sum = 0;
	private int group2Sum = 0;
	private int group3Sum = 0;
	private int group4Sum = 0;
	private int group5Sum = 0;
	
	public AdvancedPainRatingFragment() {
		// Required empty public constructor
	}

	@SuppressLint("NewApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_advanced_pain_rating, container, false);
		
		totalSumTextView = (TextView) view.findViewById(R.id.advanced_pain_rating_score);
		TextView childName = (TextView) view.findViewById(R.id.child_name_textview);
		
		RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.radio_btn_group1);
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch(checkedId) {
				case R.id.group1_radio_btn_1:
					group1Sum = 0;
					break;
				case R.id.group1_radio_btn_2:
					group1Sum = 1;
					break;
				case R.id.group1_radio_btn_3:
					group1Sum = 2;
					break;
				}
				
				calcTotal();
			}
		});
		radioGroup = (RadioGroup) view.findViewById(R.id.radio_btn_group2);
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch(checkedId) {
				case R.id.group2_radio_btn_1:
					group2Sum = 0;
					break;
				case R.id.group2_radio_btn_2:
					group2Sum = 1;
					break;
				case R.id.group2_radio_btn_3:
					group2Sum = 2;
					break;
				}
				
				calcTotal();
			}
		});
		radioGroup = (RadioGroup) view.findViewById(R.id.radio_btn_group3);
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch(checkedId) {
				case R.id.group3_radio_btn_1:
					group3Sum = 0;
					break;
				case R.id.group3_radio_btn_2:
					group3Sum = 1;
					break;
				case R.id.group3_radio_btn_3:
					group3Sum = 2;
					break;
				}
				
				calcTotal();
			}
		});
		radioGroup = (RadioGroup) view.findViewById(R.id.radio_btn_group4);
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch(checkedId) {
				case R.id.group4_radio_btn_1:
					group4Sum = 0;
					break;
				case R.id.group4_radio_btn_2:
					group4Sum = 1;
					break;
				case R.id.group4_radio_btn_3:
					group4Sum = 2;
					break;
				}
				
				calcTotal();
			}
		});
		radioGroup = (RadioGroup) view.findViewById(R.id.radio_btn_group5);
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch(checkedId) {
				case R.id.group5_radio_btn_1:
					group5Sum = 0;
					break;
				case R.id.group5_radio_btn_2:
					group5Sum = 1;
					break;
				case R.id.group5_radio_btn_3:
					group5Sum = 2;
					break;
				}
				
				calcTotal();
			}
		});
		
		ChildModel child = ChildSingleton.getChild();
		if (child != null)
			childName.setText(child.Name);
		
		return view;
	}

	private void calcTotal() {
		int totalSum = group1Sum + group2Sum + group3Sum + group4Sum + group5Sum;
		totalSumTextView.setText(totalSum + "");
	}
	
}
